package com.example.casper.Experiment2024.View;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.example.casper.Experiment2024.R;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;

public class GameView extends SurfaceView implements Runnable {
    private Thread gameThread;
    private boolean isPlaying;
    private SurfaceHolder surfaceHolder;
    private Paint paint;
    private int score;
    private long startTime;
    private long elapsedTime;
    private ArrayList<Bomb> bombs;
    private float submarineX;
    private float submarineY;
    private Bitmap submarineBitmap;
    private Bitmap bombBitmap;
    private int bombFrequency; // 炸弹生成频率
    private int bombSpeed; // 炸弹下落速度

    public GameView(Context context) {
        super(context);
        GameInit();
    }
    public GameView(Context context, AttributeSet attrs) {
        super(context, attrs);
        GameInit();
    }

    public void GameInit() {
        surfaceHolder = getHolder();
        paint = new Paint();
        score = 0;
        bombs = new ArrayList<>();
        submarineBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.submarine);
        bombBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.boom);
        submarineBitmap = Bitmap.createScaledBitmap(submarineBitmap, 100, 100, true); // 根据需求调整大小
        bombBitmap = Bitmap.createScaledBitmap(bombBitmap, 50, 50, true); // 根据需求调整大小
        submarineX = getWidth() / 2 - submarineBitmap.getWidth() / 2; // 初始潜艇位置 (水平居中)
        submarineY = 500; // 初始潜艇位置 (位于屏幕底部)
        bombFrequency = 1000; // 初始炸弹生成频率（毫秒）
        bombSpeed = 5; // 初始炸弹下落速度
        startTime = System.currentTimeMillis();
        isPlaying = false; // 初始化游戏状态

    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // 获取手指的 X 和 Y 坐标
        float touchX = event.getX();
        float touchY = event.getY();

        // 控制潜艇的上浮和下沉
        if (touchY < getHeight() / 2) {
            submarineY -= 10; // 上浮
        } else {
            submarineY += 10; // 下沉
        }

        // 控制潜艇的左右移动
        if (touchX < getWidth() / 2) {
            submarineX -= 10; // 左移
        } else {
            submarineX += 10; // 右移
        }

        // 确保潜艇不会移出屏幕边界
        if (submarineY < 0) submarineY = 0;
        else if (submarineY > getHeight() - submarineBitmap.getHeight()) submarineY = getHeight() - submarineBitmap.getHeight();

        if (submarineX < 0) submarineX = 0;
        else if (submarineX > getWidth() - submarineBitmap.getWidth()) submarineX = getWidth() - submarineBitmap.getWidth();

        return true;
    }

    @Override
    public void run() {
        bombs = new ArrayList<>();
        isPlaying = true; // 开始游戏
        while (isPlaying) {
            update();
            draw();
            control();
        }
    }

    private void update() {
        elapsedTime = System.currentTimeMillis() - startTime;

        // 每隔一段时间增加炸弹生成频率和速度
        if (elapsedTime > 10000) { // 每10秒增加一次
            bombFrequency = Math.max(500, bombFrequency - 100); // 最小频率为500ms
            bombSpeed += 2; // 每次增加下落速度
            startTime = System.currentTimeMillis(); // 重置计时器
        }

        // 根据时间生成炸弹
        if (elapsedTime > bombFrequency) {
            bombs.add(new Bomb(bombSpeed));
            startTime = System.currentTimeMillis(); // 重置计时器
        }

        // 更新炸弹位置
        Iterator<Bomb> iterator = bombs.iterator();
        while (iterator.hasNext()) {
            Bomb bomb = iterator.next();
            bomb.move();
            if (bomb.y > getHeight()) {
                score++; // 躲过一颗炸弹
                iterator.remove();
            } else if (bomb.collisionDetect(submarineX, submarineY)) {
                isPlaying = false; // 被炸弹击中，游戏结束
            }
        }
    }

    private void draw() {
        if (surfaceHolder.getSurface().isValid()) {
            Canvas canvas = surfaceHolder.lockCanvas();
            canvas.drawColor(Color.WHITE); // 清屏
            paint.setColor(Color.BLACK);
            paint.setTextSize(50);
            canvas.drawText("Score: " + score, 50, 50, paint);

            // 绘制潜艇
            canvas.drawBitmap(submarineBitmap, submarineX, submarineY, paint);

            // 绘制炸弹
            for (Bomb bomb : bombs) {
                bomb.draw(canvas, paint);
            }

            surfaceHolder.unlockCanvasAndPost(canvas);
        }
    }

    private void control() {
        try {
            Thread.sleep(17); // 控制帧率
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void pause() {
        isPlaying = false;
        try {
            gameThread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void resume() {
        isPlaying = true;
        gameThread = new Thread(this);
        gameThread.start();
    }

    // 炸弹类
    private class Bomb {
        float x, y;
        float speed;
        private boolean isDead;

        public Bomb(float speed) {
            this.x = (float) Math.random() * (getWidth() - bombBitmap.getWidth());
            this.y = -bombBitmap.getHeight(); // 从屏幕上方开始掉落
            this.speed = speed; // 使用传入的速度
        }

        public void move() {
            y += speed; // 向下移动
        }

        public boolean collisionDetect(float submarineX, float submarineY) {
            return (x < submarineX + submarineBitmap.getWidth() && x + bombBitmap.getWidth() > submarineX &&
                    y < submarineY + submarineBitmap.getHeight() && y + bombBitmap.getHeight() > submarineY);
        }

        public void draw(Canvas canvas, Paint paint) {
            canvas.drawBitmap(bombBitmap, x, y, paint);
        }
    }

    // 潜艇类

}
